import 'dart:async';

import 'package:flutter/material.dart';

class ArticleListPage extends StatefulWidget {
  String id;

  ArticleListPage(this.id);

  @override
  State<StatefulWidget> createState() {
    return new ArticleListPageState();
  }
}

class ArticleListPageState extends State<ArticleListPage>
    with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;

  //  若当前tab切到任意非相邻tab(如:第一个tab切换到第三个)，会报错，请升级flutter版本
  int curPage = 0;

  Map<String, String> map = new Map();
  List listData = new List();
  int listTotalSize = 0;
  ScrollController _contraller = new ScrollController();

  @override
  void initState() {
    super.initState();

    _getArticleList();

    _contraller.addListener(() {
      var maxScroll = _contraller.position.maxScrollExtent;
      var pixels = _contraller.position.pixels;

      if (maxScroll == pixels && listData.length < listTotalSize) {
        _getArticleList();
      }
    });
  }

  @override
  void dispose() {
    _contraller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    if ((listData == null || listData.isEmpty) && curPage == 1) {
      return new Center(
        child: new CircularProgressIndicator(),
      );
    } else {
      Widget listView = new ListView.builder(
        key: new PageStorageKey(widget.id),
        itemCount: listData.length,
        itemBuilder: (context, i) => buildItem(i),
        controller: _contraller,
      );
      return new RefreshIndicator(child: listView, onRefresh: _pullToRefresh);
    }
  }

  /*
   * 获取页面数据
   * */
  void _getArticleList() {}

  Future<Null> _pullToRefresh() async {
    curPage = 0;
    _getArticleList();
    return null;
  }

  Widget buildItem(int i) {
//    var itemData = listData[i];
//
//    if (i == listData.length - 1 &&
//        itemData.toString() == Constants.END_LINE_TAG) {
//      return new EndLine();
//    }

    return new DemoWidget();
  }
}

class DemoWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new DemoState();
  }
}

class DemoState extends State<DemoWidget> {
  @override
  Widget build(BuildContext context) {
    return new ListView();
  }
}
